package com.ssm.jvm.classloader;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class MethodHandle {
	@Override
	public String toString() {
 
		return super.toString() + "==MethodHandle";
	}
 
	public static void main(String[] args) throws Throwable {
		MethodHandle handle = new MethodHandle();
		MethodType methodType = MethodType.methodType(String.class);
 
		MethodHandles.Lookup lookup = MethodHandles.lookup();
 
		java.lang.invoke.MethodHandle methodHandle = lookup.findVirtual(MethodHandle.class, "toString", methodType);
		String toString = (String) methodHandle.invokeExact(handle);
		System.out.println(toString);// com.ssm.jvm.classloader.MethodHandle@8efb846==MethodHandle
 
		// or like this:
		java.lang.invoke.MethodHandle methodHandle2 = methodHandle.bindTo(handle);
		String toString2 = (String) methodHandle2.invokeWithArguments();
		System.out.println(toString2);// com.ssm.jvm.classloader.MethodHandle@8efb846==MethodHandle
 
		// 得到当前Class的不同表示方法，最后一个最好。一般我们在静态上下文用SLF4J得到logger用。
		System.out.println(MethodHandle.class);
		System.out.println(handle.getClass());
		System.out.println(MethodHandles.lookup().lookupClass()); // like getClass()
	}
 
}